package com.pcxt.test.test01;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Vector;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.pcxt.exception.*;
import com.pcxt.database.*;
import com.pcxt.net.*;
import com.pcxt.xml.*;

public class MainActivity extends Activity implements IDataCallBack{
	
	/*
	 * private variables
	 */
	private HttpConnectionProcessor httpConnectionProcessor = null;
	private XMLProcessor xmlProcessor = null;
	
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onLowMemory()
	 */
	
    @Override
	public void onLowMemory() {
		// TODO Auto-generated method stub
		super.onLowMemory();
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d("onCreate","onCreate Method is excuted.");
        
        HttpConnectionDelegate delegate = HttpConnectionDelegate.initialize(this, "demo", "demo", "com.dyndns-home.x10wind.Windchill");
        
       
        
        try {
        	
        	delegate.connect("http://192.168.75.128/Windchill/servlet/IE/tasks/com/ptc/windchill/foundation/dca-QueryLinks.xml?supporting-adapter=com.dyndns-home.x10wind.Windchill&dca_object_ref=VR:wt.part.WTPart:33805:087785013-1295848343564-9506622-128-134-168-192@x10wind.dyndns-home.com&direction=uses&dca_type=wt.part.WTPartUsageLink&group_out=links");
        	//httpConnectionProcessor.getRemoteFileContents("obid");

        	
        }

        catch (XException xe) {
        	//xe.printStackTrace();
        }
        
    }

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#refreshData(org.apache.http.HttpResponse)
	 */
	public void refreshData(HttpResponse response) {
		// TODO Auto-generated method stub
		Vector<HashMap<String,Object>> result = null;
        try {

        	/*
        	BufferedReader r = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        	StringBuilder total = new StringBuilder();
        	String line;
        	while ((line = r.readLine()) != null) {
        	    total.append(line);
        	}
        	*/
        	xmlProcessor = XMLProcessor.initialize(response.getEntity().getContent());
        	result = xmlProcessor.getInstances();
        	/*
        	for (int i = 0; i < result.size(); i ++) {
        		HashMap<String, Object> map = result.get(i);
        		Log.d("name",map.get("name").toString());
        	}
        	*/
        	HashMap<String, Object> map = result.get(0);
        	
        	Object[] abcd = map.keySet().toArray();
        	
        	for (int i = 0; i < abcd.length; i ++){
        		String key = (String)abcd[i];
        		Log.d(key,map.get(key).toString());
        	
        	}
        	
            //System.out.println(total.toString());
            //Log.d("onNetwork", total.toString());
            //response.getEntity().getContent().close();
        	
        }
        catch (XException xe) {
        	Log.e("XMLProcessor",xe.getMessage());
        }
        catch (IOException ioe) {
        }
		Log.d("onNetwork", "Response received.");
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getParams(java.lang.String)
	 */
	public HttpPost getParams(String obid) {
		// TODO Auto-generated method stub
	
		//HttpPost post = new HttpPost("http://192.168.75.128/Windchill/servlet/IE/tasks/com/pcxt/worklist/todolist.xml");
		HttpPost post = new HttpPost("http://192.168.75.128/Windchill/servlet/IE/tasks/com/ptc/windchill/foundation/dca-QueryLinks.xml?supporting-adapter=com.dyndns-home.x10wind.Windchill&dca_object_ref=VR:wt.part.WTPart:33805:087785013-1295848343564-9506622-128-134-168-192@x10wind.dyndns-home.com&direction=uses&dca_type=wt.part.WTPartUsageLink&group_out=links");
		HttpParams params = post.getParams();
		params.setParameter("TEST", "OK");
		return post;
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getUserid()
	 */
	public String getUserid() {
		// TODO Auto-generated method stub
		return "demo";
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getPassword()
	 */
	public String getPassword() {
		// TODO Auto-generated method stub
		return "demo";
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IHttpReceiver#getProcessorName()
	 */
	public String getProcessorName() {
		// TODO Auto-generated method stub
		return "com.dyndns-home.x10wind.Windchill";
	}

	/* (non-Javadoc)
	 * @see com.pcxt.net.IDataCallBack#received(java.util.Vector)
	 */
	public void received(Vector<HashMap<String, Object>> groups)
			throws XException {
		// TODO Auto-generated method stub
		
    	HashMap<String, Object> map = groups.get(0);
    	
    	Object[] abcd = map.keySet().toArray();
    	
    	for (int i = 0; i < abcd.length; i ++){
    		String key = (String)abcd[i];
    		Log.d(key,map.get(key).toString());
    	
    	}
		
	}
}